package com.example.logining.handler;

import cn.hutool.json.JSONUtil;
import com.example.logining.domain.result.Result;
import com.example.logining.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @title: AuthenticationEntryPointImpl
 * @author: wywang
 * @date: 2024/11/17
 * @description: 自定义认证失败异常处理类
 **/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        Result<String> result = Result.error("认证失败请重新登录", 401);
        String jsonStr = JSONUtil.toJsonStr(result);
        WebUtils.renderString(response, jsonStr);
    }
}
